﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace Project_KhuyenMaiVang.DAO
{
    public class SanPhamDAO : MyDataProvider
    {
        /// <summary>
        /// Lấy danh sách các sản phần thuộc về 1 IDAccount input
        /// </summary>
        /// <param name="IDAccount"></param>
        /// <returns>List SanPhamDTO</returns>
        public List<SanPhamDTO> GetListSanPhamByIDAccount(int IDAccount)
        {
            List<SanPhamDTO> result = new List<SanPhamDTO>();
            try
            {
                string QueryString = @"select * from SanPham where IDAccount = " + IDAccount;
                _Adapter = new SqlDataAdapter(QueryString, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                //ArrayList arr = ConvertDataSetToArrayList(dataset);
                DataTable dt = dataset.Tables[0];               

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((SanPhamDTO)ob);
                }
                
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return result;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            SanPhamDTO sanPhamDTO = new SanPhamDTO();

            sanPhamDTO.MaLoaiSanPham = Int32.Parse(dt.Rows[i]["MaLoaiSanPham"].ToString());
            sanPhamDTO.MaSanPham = Int32.Parse(dt.Rows[i]["MaSanPham"].ToString());
            sanPhamDTO.IDAccount = Int32.Parse(dt.Rows[i]["IDAccount"].ToString());
            sanPhamDTO.TenSanPham = dt.Rows[i]["TenSanPham"].ToString();
            sanPhamDTO.MoTaChiTiet = dt.Rows[i]["MoTaChiTiet"].ToString();
            sanPhamDTO.GiaGoc = float.Parse(dt.Rows[i]["GiaGoc"].ToString());

            return (object)sanPhamDTO;
        }

        // Lay ma cua san pham khi truyen thong tin san pham vao
        public int getMaSanPham(SanPhamDTO sanPhamDTO)
        {
            int result = -1;
            try
            {
                string QueryString = @"select * from SanPham where IDAccount = " + sanPhamDTO.IDAccount 
                                                                  +" and TenSanPham = N'" + sanPhamDTO.TenSanPham
                                                                  +"' and MaLoaiSanPham = " + sanPhamDTO.MaLoaiSanPham
                                                                  +" and MoTaChiTiet = N'" + sanPhamDTO.MoTaChiTiet +"'";
               
                _Adapter = new SqlDataAdapter(QueryString, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);

                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                sanPhamDTO = (SanPhamDTO)ob;
                result = sanPhamDTO.MaSanPham;
            }
            catch (System.Exception ex)
            {
                result = -1;
            }
            return result;
        }
        // Thêm 1 sản phẩm mới
        // parameter    : SanPhamDTO
        // return true nếu thành công, false nếu thất bại
        public bool insert(SanPhamDTO product)
        {
            bool result = true;
            try
            {
                string strSql = "INSERT INTO SANPHAM (TenSanPham, MaLoaiSanPham, MoTaChiTiet, GiaGoc, IDAccount) VALUES (@TenSanPham, @MaLoaiSanPham, @MoTaChiTiet, @GiaGoc, @IDAccount)";
                _Command = new SqlCommand(strSql, _Connection);
                SqlParameter[] param = new SqlParameter[5];

                param[0] = new SqlParameter("@TenSanPham", SqlDbType.NVarChar, 50);
                param[1] = new SqlParameter("@MaLoaiSanPham", SqlDbType.Int);
                param[2] = new SqlParameter("@MotaChiTiet", SqlDbType.NVarChar, Int32.MaxValue);
                param[3] = new SqlParameter("@GiaGoc", SqlDbType.Float);
                param[4] = new SqlParameter("@IDAccount", SqlDbType.Int);

                param[0].Value = product.TenSanPham;
                param[1].Value = product.MaLoaiSanPham;
                param[2].Value = product.MoTaChiTiet;
                param[3].Value = product.GiaGoc;
                param[4].Value = product.IDAccount;

                for (int i = 0; i < param.Length; i++)
                {
                    _Command.Parameters.Add(param[i]);
                }

                _Command.CommandType = CommandType.Text;
                _Command.ExecuteNonQuery();
            }
            catch
            {
                result = false;
            }

            return result;
        }

        public int themSanPham(SanPhamDTO product)
        {
            int result = -1;
            try
            {
                string strSql = "INSERT INTO SANPHAM (TenSanPham, MaLoaiSanPham, MoTaChiTiet, GiaGoc, IDAccount) VALUES (@TenSanPham, @MaLoaiSanPham, @MoTaChiTiet, @GiaGoc, @IDAccount)";
                _Command = new SqlCommand(strSql, _Connection);
                SqlParameter[] param = new SqlParameter[5];

                param[0] = new SqlParameter("@TenSanPham", SqlDbType.NVarChar, 50);
                param[1] = new SqlParameter("@MaLoaiSanPham", SqlDbType.Int);
                param[2] = new SqlParameter("@MotaChiTiet", SqlDbType.NVarChar, Int32.MaxValue);
                param[3] = new SqlParameter("@GiaGoc", SqlDbType.Float);
                param[4] = new SqlParameter("@IDAccount", SqlDbType.Int);

                param[0].Value = product.TenSanPham;
                param[1].Value = product.MaLoaiSanPham;
                param[2].Value = product.MoTaChiTiet;
                param[3].Value = product.GiaGoc;
                param[4].Value = product.IDAccount;

                for (int i = 0; i < param.Length; i++)
                {
                    _Command.Parameters.Add(param[i]);
                }
                _Command.CommandType = CommandType.Text;
                _Command.ExecuteNonQuery();
                result = getMaSanPham(product);
            }
            catch
            {
                result = -1;
            }
            
            return result;
        }




        public SanPhamDTO getSanPham(int id)
        {
            List<SanPhamDTO> result = new List<SanPhamDTO>();
            try
            {
                string QueryString = @"select * from SanPham where MaSanPham = " + id;
                _Adapter = new SqlDataAdapter(QueryString, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                //ArrayList arr = ConvertDataSetToArrayList(dataset);
                DataTable dt = dataset.Tables[0];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((SanPhamDTO)ob);
                }

            }
            catch (System.Exception ex)
            {
                return null;
            }
            return result[0];
        }
    }
}